﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Application.Services;
using Domaine.Contracts;
using Infrastructure.Data.Repository;
using Infrastructure.Data.UnitOfWork;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Application.MembershipModule.test
{
    [TestClass]
    public class AppMembershipServiceTests
    {


        [TestMethod]
        public void GetMemmberByUserNameTest()
        {

            var unitOfWork = new MembershipUnitOfWork();
            IMemberShipRepository memberShipRepository = new MemberShipRepository(unitOfWork);
            IUserRepository userRepository = new UserRepository(unitOfWork);
            IAspApplicationRepository aspApplicationRepository = new AspApplicationRepository(unitOfWork);
            //Arrange
            var appService = new AppMembershipService(memberShipRepository, userRepository, aspApplicationRepository);
            

            //Act

            Domaine.Entity.Membership member = appService.GetMemmberByUserName("rTest");

            //Assert
            Assert.IsNotNull(member);
            Assert.IsNotNull(member.User);
            Assert.IsTrue(member.User.UserName == "rTest");
        }
        [TestMethod]
        public void GetMemmberByUserIDTest()
        {
            //Arrange

            var idm = Guid.Parse("4B0AF510-E96A-452E-A4C4-51D26CDE6452");
            var appService = initAppservice();
            //Act

            Domaine.Entity.Membership member = appService.GetMemmberByUserID(idm);

            //Assert
            Assert.IsNotNull(member);
            Assert.IsNotNull(member.User);
            Assert.IsTrue(member.User.UserID == idm);
        }

        [TestMethod]
        public void AddMemberTest()
        {
            //Arrange
            var appService = initAppservice();
            var idm = Guid.NewGuid();
            var app = appService.GetAspApplication();
            //Act
            Domaine.Entity.Membership member = new Domaine.Entity.Membership()
            {
                User = new Domaine.Entity.User()
                {
                    UserID = idm,
                    UserName="9Test",
                    Application = app,
                    ApplicationID = app.ApplicationID,
                    LastActivityDate = DateTime.Now
                },
                UserID=idm,
                Email="4572ml@test.com",
                Password="t8888888es++++++++t",
                LastLockoutDate=DateTime.Now,
                LastLoginDate = DateTime.Now,
                LastPasswordChangedDate = DateTime.Now,
                FailedPasswordAttemptWindowStart = DateTime.Now,
                CreateDate = DateTime.Now,
                FailedPasswordAnswerAttemptWindowStart = DateTime.Now,
                FailedPasswordAnswerAttemptCount=0,
                FailedPasswordAttemptCount=0,
                IsApproved=true,
                ApplicationID = app.ApplicationID,
                PasswordSalt="1478",
                Comment="testeeeeee"
            };
             appService.AddMember(member);
            var newMember = appService.GetMemmberByUserID(idm);
            //Assert
             Assert.IsNotNull(newMember);
             Assert.IsTrue(member.UserID == idm);
        }
        

        AppMembershipService initAppservice()
        {

            var unitOfWork = new MembershipUnitOfWork();
            IMemberShipRepository memberShipRepository = new MemberShipRepository(unitOfWork);
            IUserRepository userRepository = new UserRepository(unitOfWork);
            IAspApplicationRepository aspApplicationRepository = new AspApplicationRepository(unitOfWork);
            //AppMembershipProvider _appMembershipProvider = new AppMembershipProvider(memberShipRepository, userRepository, aspApplicationRepository);


            //NameValueCollection nameValueCollections = new NameValueCollection() {
            // { "connectionStringName", "SCORMLMSConnectionStrings" },
            // { "tablePrefix", "aspnet_" } ,
            // { "enablePasswordRetrieval", "false" } ,
            // { "enablePasswordReset", "true" } ,
            // { "requiresQuestionAndAnswer", "false" } ,
            // { "requiresUniqueEmail", "false" } ,
            // { "maxInvalidPasswordAttempts", "5" } ,
            // { "minRequiredPasswordLength", "6" } ,
            // { "minRequiredNonalphanumericCharacters", "0" } ,
            // { "passwordAttemptWindow", "10" } ,
            // { "applicationName", "/" } ,
            // };
            //_appMembershipProvider.Initialize("AppMembershipProvider", nameValueCollections);
            return new AppMembershipService(memberShipRepository, userRepository, aspApplicationRepository);

        }



        string RandomStr()
        {
            string rStr = System.IO.Path.GetRandomFileName();
            rStr = rStr.Replace(".", ""); // For Removing the .
            return rStr;
        }
    }
}
